package com.mikufufu.blog.config;

import com.mikufufu.blog.common.language.HeaderLocaleResolver;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Locale;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public LocaleResolver localeResolver() {
        return new HeaderLocaleResolver(); // 替换默认解析器
    }

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
        source.setBasenames("classpath:i18n/messages");
        source.setDefaultEncoding("UTF-8");
        // 关键设置：将中文设为默认语言
        source.setDefaultLocale(Locale.CHINA);
        // 设置使用code作为默认消息
        source.setUseCodeAsDefaultMessage(true);
        // 禁用回退到系统语言
        source.setFallbackToSystemLocale(false);
        return source;
    }
}
